package com.fit.utils.se;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据转换工具
 * @author hcl
 *
 */
public class RecordUtil {
	private static Logger log = LoggerFactory.getLogger(RecordUtil.class);
	/**
	 * 获取字符串
	 * @return
	 */
	public static String getString(Object data){
		String result = null;
		try{
			if(data != null){
				if(data instanceof String){
					result = (String)data;
				}else{
					result = data.toString();
				}
			}
		}catch(Exception e){
			log.error("转换数据为String异常!", e);
			throw new RuntimeException("转换数据为String异常!", e);
		}
		return result;
	}
	
	
	/**
	 * 获取Int
	 * @return
	 */
	public static Integer getInteger(Object data){
		Integer result = null;
		try{
			if(data != null){
				if(data instanceof Integer){
					result = (Integer)data;
				}else{
					result = Integer.parseInt(data.toString(),10);
				}
			}
		}catch(Exception e){
			log.error("转换数据为Integer异常!", e);
			throw new RuntimeException("转换数据为Integer异常!", e);
		}
		return result;
	}
	
	/**
	 * 获取Byte
	 * @return
	 */
	public static Byte getByte(Object data){
		Byte result = null;
		try{
			if(data != null){
				if(data instanceof Byte){
					result = (Byte)data;
				}else{
					result = Byte.parseByte(data.toString(),10);
				}
			}
		}catch(Exception e){
			log.error("转换数据为Byte异常!", e);
			throw new RuntimeException("转换数据为Byte异常!", e);
		}
		return result;
	}
	
	/**
	 * 获取Short
	 * @param data
	 * @return
	 */
	public static Short getShort(Object data){
		Short result = null;
		try{
			if(data != null){
				if(data instanceof Short){
					result = (Short)data;
				}else{
					result = Short.parseShort(data.toString(),10);
				}
			}
		}catch(Exception e){
			log.error("转换数据为Short异常!", e);
			throw new RuntimeException("转换数据为Short异常!", e);
		}
		return result;
	}
	
	
	/**
	 * 获取Long
	 * @param data
	 * @return
	 */
	public static Long getLong(Object data){
		Long result = null;
		try{
			if(data != null){
				if(data instanceof Long){
					result = (Long)data;
				}else{
					result = Long.parseLong(data.toString(),10);
				}
			}
		}catch(Exception e){
			log.error("转换数据为Long异常!", e);
			throw new RuntimeException("转换数据为Long异常!", e);
		}
		return result;
	}
	
	
	/**
	 * 获取Float
	 * @return
	 */
	public static Float getFloat(Object data){
		Float result = null;
		try{
			if(data != null){
				if(data instanceof Float){
					result = (Float)data;
				}else{
					result = Float.parseFloat(data.toString());
				}
			}
		}catch(Exception e){
			log.error("转换数据为Float异常!", e);
			throw new RuntimeException("转换数据为Float异常!", e);
		}
		return result;
	}
	
	
	/**
	 * 获取Double
	 * @return
	 */
	public static Double getDouble(Object data){
		Double result = null;
		try{
			if(data != null){
				if(data instanceof Double){
					result = (Double)data;
				}else{
					result = Double.parseDouble(data.toString());
				}
			}
		}catch(Exception e){
			log.error("转换数据为Double异常!", e);
			throw new RuntimeException("转换数据为Double异常!", e);
		}
		return result;
	}
	
	/**
	 * 获取Boolean
	 * @param data
	 * @return
	 */
	public static Boolean getBoolean(Object data){
		Boolean result = null;
		try{
			if(data != null){
				if(data instanceof Boolean){
					result = (Boolean)data;
				}else if(StringUtil.isBoolean(data.toString())){
					result = Boolean.parseBoolean(data.toString());
				}
			}
		}catch(Exception e){
			log.error("转换数据为Boolean异常!", e);
			throw new RuntimeException("转换数据为Boolean异常!", e);
		}
		return result;
	}
	
	/**
	 * 获取Character
	 * @param data
	 * @return
	 */
	public static Character getCharacter(Object data){
		Character result = null;
		try{
			if(data != null){
				if(data instanceof Character){
					result = (Character)data;
				}else{
					result = data.toString().charAt(0);
				}
			}
		}catch(Exception e){
			log.error("转换数据为Character异常!", e);
			throw new RuntimeException("转换数据为Character异常!", e);
		}
		return result;
	}
	
	
	/**
	 * 获取数据
	 * @param data
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T get(Object data,Class<T> clazz){
		T result = null;
		try{
			if(data != null){
				if(clazz.isAssignableFrom(data.getClass())){
					result = (T)data;
				}
			}
		}catch(Exception e){
			log.error("转换数据为 {} 异常!",clazz.getName(), e);
			throw new RuntimeException("转换数据为 "+clazz.getName()+" 异常!", e);
		}
		return result;
	}
}
